package cn.ibizlab.odoo.core.odoo_mro.service.impl;

import java.io.Serializable;
import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.HashMap;
import java.math.BigInteger;
import java.util.Collection;

import lombok.extern.slf4j.Slf4j;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cglib.beans.BeanCopier;
import org.springframework.stereotype.Service;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageImpl;
import org.springframework.data.domain.Pageable;
import cn.ibizlab.odoo.core.client.model.Imro_pm_meter;
import cn.ibizlab.odoo.core.odoo_mro.domain.Mro_pm_meter;
import cn.ibizlab.odoo.core.odoo_mro.filter.Mro_pm_meterSearchContext;
import cn.ibizlab.odoo.core.odoo_mro.service.IMro_pm_meterService;
import cn.ibizlab.odoo.util.SearchResult;
import cn.ibizlab.odoo.util.log.IBIZLog;

/**
 * 实体[Asset Meters] 服务对象接口实现
 */
@Slf4j
@IBIZLog
@Service
public class Mro_pm_meterServiceImpl implements IMro_pm_meterService {

    @Autowired
    private cn.ibizlab.odoo.core.client.service.Imro_pm_meterClientService mro_pm_meterClientService;

    @Override
    public void updateBatch(List<Mro_pm_meter> mro_pm_meters, int batchSize) {

        //mro_pm_meterClientService.updateBatch(mro_pm_meters,batchSize) ;

    }

    @Override
    public boolean update(Mro_pm_meter mro_pm_meter) {
        boolean bOk = false ;
        Imro_pm_meter clientModel = this.convert2Model(mro_pm_meter,null);
        mro_pm_meterClientService.update(clientModel) ;
        this.convert2Domain(clientModel,mro_pm_meter) ;
        bOk = true;
        return bOk ;
    }

    @Override
    public boolean remove(Integer id) {
        boolean bOk = false ;
        Imro_pm_meter clientModel = mro_pm_meterClientService.createModel();
        clientModel.setId(id);
        mro_pm_meterClientService.remove(clientModel) ;
        bOk = true ;
        return bOk ;
    }

    @Override
    public Mro_pm_meter get(Integer id) {
        Imro_pm_meter clientModel = mro_pm_meterClientService.createModel();
        clientModel.setId(id);
        mro_pm_meterClientService.get(clientModel) ;
        return this.convert2Domain(clientModel,null);
    }

    @Override
    public Mro_pm_meter get(Mro_pm_meter mro_pm_meter) {
        Mro_pm_meter retmro_pm_meter = this.get(mro_pm_meter.getId()) ;
    	BeanCopier copier=BeanCopier.create(retmro_pm_meter.getClass(), mro_pm_meter.getClass(), false);
        copier.copy(retmro_pm_meter, mro_pm_meter, null);
        return mro_pm_meter ;
    }

    @Override
    public void createBatch(List<Mro_pm_meter> mro_pm_meters, int batchSize) {

                        //mro_pm_meterClientService.createBatch(mro_pm_meters,batchSize) ;

    }

    @Override
    public boolean create(Mro_pm_meter mro_pm_meter) {
        boolean bOk = false ;
        Imro_pm_meter clientModel = this.convert2Model(mro_pm_meter,null);
        mro_pm_meterClientService.create(clientModel) ;
        this.convert2Domain(clientModel,mro_pm_meter);
        bOk = true ;

        return bOk ;
    }

    @Override
    public void removeBatch(Collection<? extends Serializable> idList, int batchSize) {

        //mro_pm_meterClientService.removeBatch(mro_pm_meters) ;

    }

    /**
     * 查询集合 默认查询
     */
    @Override
    public Page<Mro_pm_meter> searchDefault(Mro_pm_meterSearchContext context) {
        List<Mro_pm_meter> list = new ArrayList<Mro_pm_meter>();
        Page<cn.ibizlab.odoo.core.client.model.Imro_pm_meter> clientModelList = mro_pm_meterClientService.search(context) ;
        for(cn.ibizlab.odoo.core.client.model.Imro_pm_meter clientModel : clientModelList){
            list.add( this.convert2Domain(clientModel,null));
        }
        return new PageImpl<Mro_pm_meter>(list, context.getPageable(), clientModelList.getTotalElements());
    }


    /**
     * 根据Asset查询
     */
    @Override
    public Page<Mro_pm_meter> selectByOdoo_asset(Integer id){
        Mro_pm_meterSearchContext context = new Mro_pm_meterSearchContext();
        if(!org.springframework.util.StringUtils.isEmpty(id)){
            cn.ibizlab.odoo.util.SearchFieldFilter fieldFilter=new cn.ibizlab.odoo.util.SearchFieldFilter();
            fieldFilter.setParam("id");
            fieldFilter.setCondition(cn.ibizlab.odoo.util.enums.SearchFieldType.EQ);
            fieldFilter.setValue(id);
            context.getCondition().add(fieldFilter);
        }
        List<Mro_pm_meter> list = new ArrayList<Mro_pm_meter>();
        Page<cn.ibizlab.odoo.core.client.model.Imro_pm_meter> clientModelList = mro_pm_meterClientService.search(context) ;
        for(cn.ibizlab.odoo.core.client.model.Imro_pm_meter clientModel : clientModelList){
            list.add( this.convert2Domain(clientModel,null));
        }
        return new PageImpl<Mro_pm_meter>(list, context.getPageable(), clientModelList.getTotalElements());

    }

    /**
     * 根据Rules for Meter to Meter Ratio查询
     */
    @Override
    public Page<Mro_pm_meter> selectByOdoo_parent_ratio(Integer id){
        Mro_pm_meterSearchContext context = new Mro_pm_meterSearchContext();
        if(!org.springframework.util.StringUtils.isEmpty(id)){
            cn.ibizlab.odoo.util.SearchFieldFilter fieldFilter=new cn.ibizlab.odoo.util.SearchFieldFilter();
            fieldFilter.setParam("id");
            fieldFilter.setCondition(cn.ibizlab.odoo.util.enums.SearchFieldType.EQ);
            fieldFilter.setValue(id);
            context.getCondition().add(fieldFilter);
        }
        List<Mro_pm_meter> list = new ArrayList<Mro_pm_meter>();
        Page<cn.ibizlab.odoo.core.client.model.Imro_pm_meter> clientModelList = mro_pm_meterClientService.search(context) ;
        for(cn.ibizlab.odoo.core.client.model.Imro_pm_meter clientModel : clientModelList){
            list.add( this.convert2Domain(clientModel,null));
        }
        return new PageImpl<Mro_pm_meter>(list, context.getPageable(), clientModelList.getTotalElements());

    }

    /**
     * 根据Asset Meters查询
     */
    @Override
    public Page<Mro_pm_meter> selectByOdoo_parent_meter(Integer id){
        Mro_pm_meterSearchContext context = new Mro_pm_meterSearchContext();
        if(!org.springframework.util.StringUtils.isEmpty(id)){
            cn.ibizlab.odoo.util.SearchFieldFilter fieldFilter=new cn.ibizlab.odoo.util.SearchFieldFilter();
            fieldFilter.setParam("id");
            fieldFilter.setCondition(cn.ibizlab.odoo.util.enums.SearchFieldType.EQ);
            fieldFilter.setValue(id);
            context.getCondition().add(fieldFilter);
        }
        List<Mro_pm_meter> list = new ArrayList<Mro_pm_meter>();
        Page<cn.ibizlab.odoo.core.client.model.Imro_pm_meter> clientModelList = mro_pm_meterClientService.search(context) ;
        for(cn.ibizlab.odoo.core.client.model.Imro_pm_meter clientModel : clientModelList){
            list.add( this.convert2Domain(clientModel,null));
        }
        return new PageImpl<Mro_pm_meter>(list, context.getPageable(), clientModelList.getTotalElements());

    }

    /**
     * 根据Asset Parameters查询
     */
    @Override
    public Page<Mro_pm_meter> selectByOdoo_name(Integer id){
        Mro_pm_meterSearchContext context = new Mro_pm_meterSearchContext();
        if(!org.springframework.util.StringUtils.isEmpty(id)){
            cn.ibizlab.odoo.util.SearchFieldFilter fieldFilter=new cn.ibizlab.odoo.util.SearchFieldFilter();
            fieldFilter.setParam("id");
            fieldFilter.setCondition(cn.ibizlab.odoo.util.enums.SearchFieldType.EQ);
            fieldFilter.setValue(id);
            context.getCondition().add(fieldFilter);
        }
        List<Mro_pm_meter> list = new ArrayList<Mro_pm_meter>();
        Page<cn.ibizlab.odoo.core.client.model.Imro_pm_meter> clientModelList = mro_pm_meterClientService.search(context) ;
        for(cn.ibizlab.odoo.core.client.model.Imro_pm_meter clientModel : clientModelList){
            list.add( this.convert2Domain(clientModel,null));
        }
        return new PageImpl<Mro_pm_meter>(list, context.getPageable(), clientModelList.getTotalElements());

    }

    /**
     * 根据用户查询
     */
    @Override
    public Page<Mro_pm_meter> selectByOdoo_create(Integer id){
        Mro_pm_meterSearchContext context = new Mro_pm_meterSearchContext();
        if(!org.springframework.util.StringUtils.isEmpty(id)){
            cn.ibizlab.odoo.util.SearchFieldFilter fieldFilter=new cn.ibizlab.odoo.util.SearchFieldFilter();
            fieldFilter.setParam("id");
            fieldFilter.setCondition(cn.ibizlab.odoo.util.enums.SearchFieldType.EQ);
            fieldFilter.setValue(id);
            context.getCondition().add(fieldFilter);
        }
        List<Mro_pm_meter> list = new ArrayList<Mro_pm_meter>();
        Page<cn.ibizlab.odoo.core.client.model.Imro_pm_meter> clientModelList = mro_pm_meterClientService.search(context) ;
        for(cn.ibizlab.odoo.core.client.model.Imro_pm_meter clientModel : clientModelList){
            list.add( this.convert2Domain(clientModel,null));
        }
        return new PageImpl<Mro_pm_meter>(list, context.getPageable(), clientModelList.getTotalElements());

    }

    /**
     * 根据用户查询
     */
    @Override
    public Page<Mro_pm_meter> selectByOdoo_write(Integer id){
        Mro_pm_meterSearchContext context = new Mro_pm_meterSearchContext();
        if(!org.springframework.util.StringUtils.isEmpty(id)){
            cn.ibizlab.odoo.util.SearchFieldFilter fieldFilter=new cn.ibizlab.odoo.util.SearchFieldFilter();
            fieldFilter.setParam("id");
            fieldFilter.setCondition(cn.ibizlab.odoo.util.enums.SearchFieldType.EQ);
            fieldFilter.setValue(id);
            context.getCondition().add(fieldFilter);
        }
        List<Mro_pm_meter> list = new ArrayList<Mro_pm_meter>();
        Page<cn.ibizlab.odoo.core.client.model.Imro_pm_meter> clientModelList = mro_pm_meterClientService.search(context) ;
        for(cn.ibizlab.odoo.core.client.model.Imro_pm_meter clientModel : clientModelList){
            list.add( this.convert2Domain(clientModel,null));
        }
        return new PageImpl<Mro_pm_meter>(list, context.getPageable(), clientModelList.getTotalElements());

    }



    public Imro_pm_meter convert2Model(Mro_pm_meter domain , Imro_pm_meter model) {
        if(domain == null)
            return model ;
        if(model==null){
            model = mro_pm_meterClientService.createModel() ;
        }

         if(domain.getIdDirtyFlag())
            model.setId(domain.getId());
         if(domain.getMin_utilizationDirtyFlag())
            model.setMin_utilization(domain.getMin_utilization());
         if(domain.getDateDirtyFlag())
            model.setDate(domain.getDate());
         if(domain.getReading_typeDirtyFlag())
            model.setReading_type(domain.getReading_type());
         if(domain.getValueDirtyFlag())
            model.setValue(domain.getValue());
         if(domain.getMeter_line_idsDirtyFlag())
            model.setMeter_line_ids(domain.getMeter_line_ids());
         if(domain.getDisplay_nameDirtyFlag())
            model.setDisplay_name(domain.getDisplay_name());
         if(domain.getWrite_dateDirtyFlag())
            model.setWrite_date(domain.getWrite_date());
         if(domain.getView_line_idsDirtyFlag())
            model.setView_line_ids(domain.getView_line_ids());
         if(domain.getStateDirtyFlag())
            model.setState(domain.getState());
         if(domain.getCreate_dateDirtyFlag())
            model.setCreate_date(domain.getCreate_date());
         if(domain.get__last_updateDirtyFlag())
            model.set__last_update(domain.get__last_update());
         if(domain.getUtilizationDirtyFlag())
            model.setUtilization(domain.getUtilization());
         if(domain.getNew_valueDirtyFlag())
            model.setNew_value(domain.getNew_value());
         if(domain.getAv_timeDirtyFlag())
            model.setAv_time(domain.getAv_time());
         if(domain.getTotal_valueDirtyFlag())
            model.setTotal_value(domain.getTotal_value());
         if(domain.getName_textDirtyFlag())
            model.setName_text(domain.getName_text());
         if(domain.getCreate_uid_textDirtyFlag())
            model.setCreate_uid_text(domain.getCreate_uid_text());
         if(domain.getMeter_uomDirtyFlag())
            model.setMeter_uom(domain.getMeter_uom());
         if(domain.getWrite_uid_textDirtyFlag())
            model.setWrite_uid_text(domain.getWrite_uid_text());
         if(domain.getAsset_id_textDirtyFlag())
            model.setAsset_id_text(domain.getAsset_id_text());
         if(domain.getParent_ratio_id_textDirtyFlag())
            model.setParent_ratio_id_text(domain.getParent_ratio_id_text());
         if(domain.getParent_meter_idDirtyFlag())
            model.setParent_meter_id(domain.getParent_meter_id());
         if(domain.getParent_ratio_idDirtyFlag())
            model.setParent_ratio_id(domain.getParent_ratio_id());
         if(domain.getNameDirtyFlag())
            model.setName(domain.getName());
         if(domain.getWrite_uidDirtyFlag())
            model.setWrite_uid(domain.getWrite_uid());
         if(domain.getCreate_uidDirtyFlag())
            model.setCreate_uid(domain.getCreate_uid());
         if(domain.getAsset_idDirtyFlag())
            model.setAsset_id(domain.getAsset_id());
        return model ;
    }

    public Mro_pm_meter convert2Domain( Imro_pm_meter model ,Mro_pm_meter domain) {
        if(model == null)
            return domain ;
        if(domain == null){
            domain = new Mro_pm_meter();
        }

        if(model.getIdDirtyFlag())
            domain.setId(model.getId());
        if(model.getMin_utilizationDirtyFlag())
            domain.setMin_utilization(model.getMin_utilization());
        if(model.getDateDirtyFlag())
            domain.setDate(model.getDate());
        if(model.getReading_typeDirtyFlag())
            domain.setReading_type(model.getReading_type());
        if(model.getValueDirtyFlag())
            domain.setValue(model.getValue());
        if(model.getMeter_line_idsDirtyFlag())
            domain.setMeter_line_ids(model.getMeter_line_ids());
        if(model.getDisplay_nameDirtyFlag())
            domain.setDisplay_name(model.getDisplay_name());
        if(model.getWrite_dateDirtyFlag())
            domain.setWrite_date(model.getWrite_date());
        if(model.getView_line_idsDirtyFlag())
            domain.setView_line_ids(model.getView_line_ids());
        if(model.getStateDirtyFlag())
            domain.setState(model.getState());
        if(model.getCreate_dateDirtyFlag())
            domain.setCreate_date(model.getCreate_date());
        if(model.get__last_updateDirtyFlag())
            domain.set__last_update(model.get__last_update());
        if(model.getUtilizationDirtyFlag())
            domain.setUtilization(model.getUtilization());
        if(model.getNew_valueDirtyFlag())
            domain.setNew_value(model.getNew_value());
        if(model.getAv_timeDirtyFlag())
            domain.setAv_time(model.getAv_time());
        if(model.getTotal_valueDirtyFlag())
            domain.setTotal_value(model.getTotal_value());
        if(model.getName_textDirtyFlag())
            domain.setName_text(model.getName_text());
        if(model.getCreate_uid_textDirtyFlag())
            domain.setCreate_uid_text(model.getCreate_uid_text());
        if(model.getMeter_uomDirtyFlag())
            domain.setMeter_uom(model.getMeter_uom());
        if(model.getWrite_uid_textDirtyFlag())
            domain.setWrite_uid_text(model.getWrite_uid_text());
        if(model.getAsset_id_textDirtyFlag())
            domain.setAsset_id_text(model.getAsset_id_text());
        if(model.getParent_ratio_id_textDirtyFlag())
            domain.setParent_ratio_id_text(model.getParent_ratio_id_text());
        if(model.getParent_meter_idDirtyFlag())
            domain.setParent_meter_id(model.getParent_meter_id());
        if(model.getParent_ratio_idDirtyFlag())
            domain.setParent_ratio_id(model.getParent_ratio_id());
        if(model.getNameDirtyFlag())
            domain.setName(model.getName());
        if(model.getWrite_uidDirtyFlag())
            domain.setWrite_uid(model.getWrite_uid());
        if(model.getCreate_uidDirtyFlag())
            domain.setCreate_uid(model.getCreate_uid());
        if(model.getAsset_idDirtyFlag())
            domain.setAsset_id(model.getAsset_id());
        return domain ;
    }


}
